// @ts-nocheck

import health from '@blueos.health.health'

const getDataTypeMap = () => ({
  // 心率
  HEART_RATE: health.DATA_TYPES.HEART_RATE,
  // 步数
  STEP_COUNT: health.DATA_TYPES.STEP_COUNT,
  // 血氧
  SPO2: health.DATA_TYPES.SPO2,
  // 压力
  STRESS: health.DATA_TYPES.STRESS
})

export const getHealthSampleByType = (dataType) => {
  console.log(dataType, '---')
  return new Promise((resolve, reject) => {
    health.getRecentSamples({
      dataTypes: [
        getDataTypeMap()[dataType]
      ],
      success: (res) => {
        if (process.env.NODE_ENV == 'development') {
          const mockRes = [
            { "dataType": 0, "data": { "timeStamp": 1710561758, "value": String(Date.now()).slice(-2) } },
          ]
          resolve(mockRes[0].data.value)
        } else {
          resolve(res[0].data.value)
        }
      },
      fail: () => {
        console.log('fail')
        reject()
      }
    })
  })
}

let timer = null;
export const subscribeSampleByType = (dataType, callback, fail) => {
  timer = setInterval(() => {
    try {
      getHealthSampleByType(dataType).then(callback);
    } catch (err) {
      fail()
    }
  }, 4000)
}


export const unsubscribeSampleByType = () => {
  clearInterval(timer)
}